package com.leesche.zcsp.node.net.timer;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.leesche.zcsp.node.cache.JedisUtil;
import com.leesche.zcsp.node.net.service.data.NetDevice;
import com.leesche.zcsp.node.web.model.PubDevice;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDeviceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceCondition;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author reid
 * @Created 16/11/25
 */
@Component
//@Lazy(value = false)
public class DeviceTimer2 {

    private final static Logger LOGGER = Logger.getLogger(DeviceTimer.class);

    public static Map<byte[], byte[]> deviceMap = new HashMap<byte[], byte[]>();

    /**
     * protostuff 序列化
     */
    private static RuntimeSchema schema = RuntimeSchema.createFrom(NetDevice.class);

    @Autowired
    PubDeviceService pubDeviceService;

    /**
     * 定时任务,两小时执行一次,缓存所有设备信息到redis
     */
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 2)
    public void doDeviceTimer() {
        LOGGER.info("开始加载并缓存所有设备信息......");
        QueryPubDeviceCondition condition = new QueryPubDeviceCondition();
        List<PubDeviceDto> lists = pubDeviceService.findListsByCondition(condition);
//        Map<byte[], byte[]> deviceMap = new HashMap<byte[], byte[]>();
//        NetDevice message = (NetDevice) schema.newMessage();
        for (PubDeviceDto pubDeviceDto : lists) {
            NetDevice device = new NetDevice(pubDeviceDto.getDeviceid(), pubDeviceDto.getAdminid(),
                    pubDeviceDto.getPlaceid(), pubDeviceDto.getIsonline());
            deviceMap.put(pubDeviceDto.getImme().getBytes(), ProtostuffIOUtil.toByteArray(device, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)));
        }
//        JedisUtil.hmset("deviceMap", deviceMap);
    }

    /**
     * 添加单个设备信息到缓存
     * @param pubDevice
     */
    public static void addDeviceName(PubDevice pubDevice) {
        NetDevice device = new NetDevice(pubDevice.getDeviceid(), pubDevice.getAdminid(), pubDevice.getPlaceid(), pubDevice.getIsonline());
        JedisUtil.hset("deviceMap", pubDevice.getImme(), device);
    }

    /**
     * 添加单个设备信息到缓存
     * @param pubDeviceDto
     */
    public static void addDeviceName(PubDeviceDto pubDeviceDto) {
        NetDevice device = new NetDevice(pubDeviceDto.getDeviceid(), pubDeviceDto.getAdminid(), pubDeviceDto.getPlaceid(), pubDeviceDto.getIsonline());
        JedisUtil.hset("deviceMap", pubDeviceDto.getImme(), device);
    }

    /**
     * 从缓存中获取设备信息 deviceId
     * @param deviceName
     * @return
     */
    public static Long getDeviceId(String deviceName) {
        NetDevice device = (NetDevice) JedisUtil.hget("deviceMap", deviceName, new NetDevice());
        if (device != null) {
            return device.getDeviceId();
        }
        return null;
    }

    /**
     * 更新设备是否在线状态
     * @param deviceName
     * @param isOnline
     * @return
     */
    public static Long updateIsOnline(String deviceName, Integer isOnline) {
        NetDevice device = (NetDevice) JedisUtil.hget("deviceMap", deviceName, new NetDevice());
        device.setIsonline(isOnline);
        return JedisUtil.hset("deviceMap", deviceName, device);
    }

    /**
     * 获取设备
     * @param deviceName
     * @return
     */
    public static NetDevice getNetDevice(String deviceName) {
        byte[] bytes = deviceMap.get(deviceName.getBytes());
        NetDevice netDevice = new NetDevice();
        ProtostuffIOUtil.mergeFrom(bytes, netDevice, schema);
        return netDevice;
//        return (NetDevice) JedisUtil.hget("deviceMap", deviceName, new NetDevice());
    }

    /**
     * 从缓存中删除单条数据
     * @param deviceName
     * @return
     */
    public static Long removeNetDevice(String deviceName) {
        return JedisUtil.hdel("deviceMap", deviceName);
    }
}
